//
// Created by zhou on 2021/12/7.
//
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> res;
        int size=intervals.size();
        sort(intervals.begin(),intervals.end());
        for(int i=0;i<size;i++){
            int left=intervals[i][0];
            int right=intervals[i][1];

            if(i<size-1)
            while((intervals[i+1][0]<=right&&intervals[i+1][1]>right)||(intervals[i+1][0]<=right&&intervals[i+1][1]<=right)){
                if(intervals[i+1][1]>right)
                right=intervals[i+1][1];
                i++;
                if(i>=size-1)
                    break;
            }
            res.push_back({left,right});
                //localmax=
        }
        return res;
    }
};
int main(){
    vector<vector<int>> num={{1,3}};
    Solution solution;
    auto m=solution.merge(num);
    for(auto x:m){
        for(auto y:x){
            cout<<y<<",";
        }
        cout<<endl;
    }
}